package demo1;

import java.util.Arrays;
import java.util.Comparator;

class Student1 implements Comparable<Student1>{
    public String name;
    public int age;

    public Student1(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    public int compareTo(Student1 o) {
        if (this.age - o.age < 0) {
            return -1;
        }else if (this.age - o.age > 0) {
            return 1;
        }else{
            return 0;
        }
    }
}

class AgeComparable implements Comparator<Student1> {
    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.age - o2.age;
    }
}

class NameComparable implements Comparator<Student1> {
    @Override
    public int compare(Student1 o1, Student1 o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class Test {
    public static void main(String[] args) {
        Student1[] students = new Student1[3];
        students[0] = new Student1("zhangsan", 19);
        students[1] = new Student1("lisi", 17);
        students[2] = new Student1("wangwu", 20);
        AgeComparable ageComparable = new AgeComparable();//按照年龄排序
        NameComparable nameComparable = new NameComparable();//按照姓名排序
    }

    public static void main1(String[] args) {
        int[] array = {1, 4, 2, 7, 3, 8, 5};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}
